//
//  PopoverAnimator.swift
//  SwiftWB
//
//  Created by 远成物流 on 2017/6/26.
//
//

import UIKit

class PopoverAnimator: NSObject {
    var isPresented:Bool = true
    var presentedFrame = CGRect.zero
    var changeTitleBtnStatu : ((_ presented:Bool) -> ())?
    
    // MARK:- 自定义构造函数
    // 注意:如果自定义了一个构造函数,但是没有对默认构造函数init()进行重写,那么自定义的构造函数会覆盖默认的init()构造函数
    // @escaping:逃逸闭包，闭包是在函数执行完后才被调用
    // @noescaping:非逃逸闭包，闭包是在函数结束前内被调用
    init(changeTitleBtnStatu : @escaping (_ presented:Bool) -> ()) {
        self.changeTitleBtnStatu = changeTitleBtnStatu
    }
}

extension PopoverAnimator:UIViewControllerTransitioningDelegate{
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = PresentationController(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        changeTitleBtnStatu!(isPresented)
        
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        changeTitleBtnStatu!(isPresented)
        
        return self
    }
}

extension PopoverAnimator:UIViewControllerAnimatedTransitioning{
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        isPresented ? animateForPresented(transitionContext: transitionContext) : animateForDismissed(transitionContext: transitionContext)
    }
    
    //显示动画
    func animateForPresented(transitionContext: UIViewControllerContextTransitioning) {
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        transitionContext.containerView.addSubview(presentedView)
        
        presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)//设置锚点
        
        UIView.animate(withDuration: 0.3, animations: {
            
            presentedView.transform = CGAffineTransform.identity
            
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    //消失动画
    func animateForDismissed(transitionContext: UIViewControllerContextTransitioning) {
        
        let dismissedView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        
        UIView.animate(withDuration: 0.3, animations: {
            
            dismissedView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.000001)
            
        }) { (_) in
            dismissedView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
        
    }
}
